var ns, app,superFile,imageBrowser;
app = new Vue({
    el: '#app',
    data: {
        ismoreshow: false,
        ApprovalID: 0,
        // IsApproval: 1,//1--审批，0--采购
        form: {
            ID: 0,
            imagelist: [],
            processlist: [],
        },
        FormData: {},
        Title: '',
        AddTime: '',
        GoodsUseName: '',
        GoodsList: [],
        StateDesc: '',
        TrailList: [],
        IsRevoke: false,
        IsAccept: false,
        IsReject: false,
        State: 1,
        Type: 1,
        btnstatus: true,
        RoomFeeList: [],
        ProjectFullName: '',
        OwnerName: '',
        DepartmentNames: '',
        DiscountRemark: '',
        ProjectName: '',
        ChargeName: '',
        TimeRange: '',
        TotalCost: 0,
        DiscountCost: 0,
        hetong:{
            showdetail:false,
        },
        showmore:false,
        moreContent:'',
        moreName:'',
    },
    methods: {
        get_data: function () {
            var that = this;
            var options = {};
            options.ApprovalID = that.ApprovalID;
            // options.IsApproval = that.IsApproval;
            options.action = "getapprovalcenterdetail";
            console.log(JSON.stringify(options));
            ns.post(options, function (succeed, data, err) {
                console.log(JSON.stringify(data.data));
                if (succeed) {
                    that.FormData = data.data;
                    // for(var i=0;i<=that.FormData.TrailList.length;i++){
                    //     var value=that.FormData.TrailList[i].AcceptCont;
                    //     if(value&&value.length>20){
                    //         that.FormData.TrailList[i].AcceptCont=value.substring(0,20)+'...';
                    //     }
                    // }
                    // console.log(JSON.stringify(that.FormData));

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_open_housedetail: function (ID) {
            var that = this;
            ns.openWin('house_details', '房屋租售详情', {
                ID: ID,
            });
        },
        do_tongzhidetail: function (id,type) {
            var that = this;
            if(type==9||type==11||type==12){
                ns.openWin('tongzhixiaoxidetail_frm', '消息详情', {
                    id: id,
                    IsApproval: 1
                })
            }
            if(type==13){
                ns.openWin('diaochawenjuan_frm', '问卷调查详情', {
                    id: id,
                    detailtype:1
                })
            }
            if(type==14){
                ns.openWin('shequtoupiao_frm', '社区投票详情', {
                    id: id,
                    detailtype:2
                })
            }
        },
        do_linedetail: function (id) {
            ns.openWin('Line_details', '生活服务详情', {
                ID: id,
            });

        },
        open_OutLink: function (url) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确认打开第三方链接？',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        api.openWin({
                            name: 'win_show2',
                            url: url,
                            rect: {
                                x: 0,
                                y: 0,
                            }
                        })
                    };
                };
            });
        },
        //人员档案
        person_detail:function(id){
            var that=this;
            ns.openWin('person_detail', '个人档案', {
                ID: id,
            });
        },
        //合同审批-展开详细信息
        open_all:function(){
            var that=this;
            that.hetong.showdetail=true;
        },
        //合同审批-关闭详细信息，展示关键信息
        close_all:function(){
            var that=this;
            that.hetong.showdetail=false;
        },
        // 合同详情
        hetong_detail:function(id){
            var that=this;
            ns.openWin('hetong_detail', '合同详情', {
                ID: id,
            });
        },
        //外包单位详情
        othercomputer_detail:function(id){
            var that=this;
            ns.openWin('othercomputer_detail', '外包单位详情', {
                ID: id,
            });
        },
        //打开文件
        open_flie:function(item){
            var that=this;
            if(item.TypeName=='png'||item.TypeName=='jpg'||item.TypeName=='tif'||item.TypeName=='gif'){
                that.img_browser(item.FilePath);
            }else{
                that.do_openFile(item);
            }
        },
        img_browser: function(imgurl) {
            var that = this;
            var imgurls = [];
            imgurls.push(imgurl);
            imageBrowser.openImages({
                imageUrls: imgurls,
                showList: false,
                activeIndex: 0
            });
        },
        do_openFile:function(item){
            var that=this;
            api.download({
                url:item.FilePath,
                savePath:'fs://app/'+item.ImgName,
                report:true,
                cache:true,
                allowResume:true
            }, function(ret, err){
                if(ret.state==1){
                    // api.toast({
                    //     msg: '下载成功'+item.FilePath,
                    //     duration: 2000,
                    //     location: 'bottom'
                    // });
                    // if(api.systemType=='ios'){
                    //     superFile.open({
                    //         path:ret.savePath,
                    //     })
                    // }
                    if(api.systemType=="android"){
                        superFile.open({
                            path:ret.savePath,
                        })


                        // superFile.init(function(ret){
                        //     console.log(JSON.stringify(ret));
                            
                        //     if(ret.eventType=="onViewInitFinished"){
                        //         console.log(JSON.stringify('ddd'));
                                
                        //     }
                        // })
                    }
                }
            })
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            if(that.FormData.Type==19||that.FormData.Type==20){
                for (var i = 0; i < that.FormData.Attachment.length; i++) {
                    imageUrls.push(that.FormData.Attachment[i].Url);
                }
            }else{
                for (var i = 0; i < that.FormData.Picture.length; i++) {
                    imageUrls.push(that.FormData.Picture[i].ImgPath);
                }
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },

        //同意
        do_open_sure: function (ApprovalID) {
            var that = this;
            var options = {};
            options.ApprovalID = ApprovalID;
            options.Type = 1;
            options.action = "getisoperate";
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    if (data.status == true) {
                        ns.openWin('examine_yes', '审批建议', {
                            IsReject: 0,
                            ApprovalID: ApprovalID,
                        });
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }    
                }
                else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                } 
            });
        },
        //拒绝
        do_open_jujue: function (ApprovalID) {
            var that = this;
            var options = {};
            options.ApprovalID = ApprovalID;
            options.Type = 2;
            options.action = "getisoperate";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        ns.openWin('examine_yes', '审批建议', {
                            IsReject: 1,
                            ApprovalID: ApprovalID,
                        });
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },

        //撤销
        doRevoke: function (ApprovalID) {
            var that = this;
            var options = {};
            options.ApprovalID = ApprovalID;
            options.action = "approvalrevoke";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        api.toast({
                            msg: '撤销成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.btnstatus = false;
                        that.get_data();
                        api.sendEvent({
                            name: 'reloadexaminelist',
                        });
                    } else if (err) {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
                else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        set_value:function(value){
            if(value&&value.length>30){
                value=value.substring(0,30)+'...';
            }
            return value;
        },
        open_more:function(item){
            var that=this;
            that.showmore=true;
            that.moreContent=item.AcceptCont;
            that.moreName=item.AcceptName;
        },


    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    superFile = api.require('superFile');
    if (api.systemType == "android") {
        superFile.init(function(ret){});
    }
    imageBrowser = api.require('imageBrowser');
    app.ApprovalID = api.pageParam.ApprovalID;
    // app.IsApproval = api.pageParam.IsApproval;
    api.addEventListener({
        name: 'reloadexaminedetail'
    }, function (ret, err) {
        app.get_data();
    });


    // app.isviewdetail = api.pageParam.isviewdetail || false;
    // app.typetj=api.pageParam.type;
    app.get_data();
    // api.addEventListener({
    //     name: 'updata_task_con'
    // }, function(ret, err) {
    //     if (ret) {
    //         app.get_data();
    //         app.ismoreshow = false;
    //     } else {
    //         api.toast({
    //             msg: err,
    //             duration: 2000,
    //             location: 'bottom'
    //         });

    //     }
    // });

};
